cmake_minimum_required(VERSION 3.11)
project(ribbontoolbar LANGUAGES CXX)

# 设置项目版本
set(PROJECT_VERSION 1.0.0)

# 启用自动处理 Qt 元对象系统
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# 设置 C++17 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 定义动态库导出宏
add_definitions(-DRIBBONTOOLBAR_LIBRARY)

# 查找 Qt 组件
find_package(Qt5 REQUIRED COMPONENTS Core Widgets LinguistTools)

# 设置包含目录
include_directories(
    include
    ${Qt5Core_INCLUDE_DIRS}
    ${Qt5Widgets_INCLUDE_DIRS}
)

set(RCC_FILES
	${CMAKE_CURRENT_SOURCE_DIR}/image.qrc
)

# 源文件列表
set(SOURCES
    ribbontoolbar.cpp
)

# 头文件列表
set(HEADERS
    itoolbar.h
    ribbontoolbar.h
    ribbontoolbarglobal.h
)

set(TS_FILES ${CMAKE_SOURCE_DIR}/mui/zh_CN/translate_zh.ts)
set(QM_OUTPUT_DIR ${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}/mui)

qt5_create_translation(QM_FILES 
    ${SOURCES} ${HEADERS}  # 修复：使用已定义的 SOURCES 和 HEADERS 变量
    ${TS_FILES}
)

# 创建动态库
add_library(ribbontoolbar SHARED
    ${SOURCES}
    ${HEADERS}
    ${RCC_FILES}
    ${QM_FILES} 
)

# 链接 Qt 库
target_link_libraries(ribbontoolbar PRIVATE
    Qt5::Core
    Qt5::Widgets
)

add_custom_target(translations_${PROJECT_NAME} DEPENDS ${QM_FILES})
add_dependencies(${PROJECT_NAME} translations_${PROJECT_NAME})

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory ${QM_OUTPUT_DIR}
    COMMAND ${CMAKE_COMMAND} -E copy ${QM_FILES} ${QM_OUTPUT_DIR}
    COMMENT "Copying .qm files to ${QM_OUTPUT_DIR_${PROJECT_NAME}}"
)

# 设置目标属性
set_target_properties(ribbontoolbar PROPERTIES
    VERSION ${PROJECT_VERSION}
    SOVERSION 1
    OUTPUT_NAME "ribbontoolbar"
    PUBLIC_HEADER "${HEADERS}"
)

# 安装规则
install(TARGETS ribbontoolbar
    EXPORT ribbontoolbarTargets
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
    PUBLIC_HEADER DESTINATION include/ribbontoolbar
)

install(EXPORT ribbontoolbarTargets
    FILE ribbontoolbarTargets.cmake
    NAMESPACE ribbontoolbar::
    DESTINATION lib/cmake/ribbontoolbar
)

# 设置库的输出目录
set_target_properties(${PROJECT_NAME} PROPERTIES
    OUTPUT_NAME "ribbontoolbar"
    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin  #.lib .exp 输出路径
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin  #.dll 输出路径
)